type events = {
    onPreCreate: (map: string) => void,
    onClientMessage: (data: { entity: GamePlayerEntity, args: any }) => void
}

class EventBus {
    events: Map<string, Function[]> = new Map
    on<T extends keyof events>(event: T, func: events[T]) {
        let allEvent = this.events.get(event)
        if (typeof allEvent == "undefined") { this.events.set(event, []); allEvent = [] }
        allEvent?.push(func)
        this.events.set(event, allEvent!)
    }

    run(event: keyof events, data: Object) {
        this.events.get(event)?.forEach(e => { e(data) })
    }

    clear(event: string) {
        this.events.set(event, [])
    }
}

export const eventBus = new EventBus()